import { Effect, Reducer } from 'umi';

import { getPanels, getDashboard, getWidgetParams, getWidgetData } from '@/services/selfquery';

import { PanelsItem, PanelsParentItem, IDashboardItem } from '../type';

export interface PanelModelState {
  panels: Array<PanelsItem>;
  panelsParentList: Array<PanelsParentItem>;
  panelsThemeList: Array<any>
  dashboard: IDashboardItem | any;
}

export interface PanelModelType {
  namespace: 'panel';
  state: PanelModelState;
  effects: {
    fetchPanels: Effect;
    fetchDashboard: Effect;
    fetchWidgetParams: Effect;
    fetchWidgetData: Effect;
    sevaDashboard: Effect;
  };
  reducers: {
    save: Reducer;
  };
}

const PanelModel: PanelModelType = {
  namespace: 'panel',

  state: {
    panels: [],
    panelsThemeList: [],
    panelsParentList: [],
    dashboard: {},
  },

  effects: {
    // 获取看板列表
    *fetchPanels({ callback }, { call, put }: any) {
      let response = yield call(getPanels);
      let treeData: Array<PanelsParentItem> = [];
      let themeTreeData: Array<any> = [];

      if (response && response.payload) {
        response.payload = response.payload.filter(
          (v: any) => v.h5Widgets && v.h5Widgets.length > 0,
        );
        // 获取树形结构父级 项目级
        response.payload.map((item: PanelsItem) => {
          const obj = {
            projectId: item.projectId,
            projectName: item.projectName,
          };
          if (JSON.stringify(treeData).indexOf(JSON.stringify(obj)) === -1) {
            treeData.push(obj);
          }
        });

        // 获取树形结构父级 主题级
        response.payload.map((item: PanelsItem) => {
          const obj = {
            projectId: item.projectId,
            dashboardPortalId: item.dashboardPortalId,
            dashboardPortalName: item.dashboardPortalName,
          };
          if (JSON.stringify(themeTreeData).indexOf(JSON.stringify(obj)) === -1) {
            themeTreeData.push(obj);
          }
        });

        // 默认回调请求第一个指标详情
        if (callback) callback(response.payload);
      }

      // 看板列表
      yield put({
        type: 'save',
        payload: { panels: response.payload },
      });

      // 项目列表
      yield put({
        type: 'save',
        payload: { panelsParentList: treeData },
      });

      // 主题列表
      yield put({
        type: 'save',
        payload: { panelsThemeList: themeTreeData },
      });
    },

    // 获取指标列表
    *fetchDashboard({ payload, callback }: any, { call, put }: any) {
      const response = yield call(getDashboard, payload);
      yield put({
        type: 'save',
        payload: { dashboard: response.payload },
      });
      if (callback) callback(response.payload);
    },

    // 更改指标列表数据
    *sevaDashboard({ payload }: any, {  put }: any) {
      yield put({
        type: 'save',
        payload: { dashboard: payload },
      });
    },

    // 获取指标图表数据所需参数
    *fetchWidgetParams({ payload, callback }, { call }) {
      const response = yield call(getWidgetParams, payload);

      if (callback) callback(response);
    },

    // 获取指标图表数据
    *fetchWidgetData({ payload, callback }, { call }) {
      const response = yield call(getWidgetData, payload);

      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default PanelModel;
